#    This file is part of OpenPolitik.
#
#    OpenPolitik is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    OpenPolitik is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with OpenPolitik.  If not, see <http://www.gnu.org/licenses/>.
import os
import re
import urllib

import session

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template

import op_models
import helpers

class Give404(webapp.RequestHandler):
	def get(self):
		template_values = helpers.get_liu_stuff(self.request.uri)

		self.response.set_status(404)
		self.response.out.write(template.render(helpers.get_path("_404"), template_values))

class Message(webapp.RequestHandler):
	def get(self):
		template_values = helpers.get_liu_stuff(self.request.uri)
		s = session.Session()

		if s["message"] == "":
			self.redirect("/")
			return

		template_values["message"] = s["message"]
		s["message"] = ""

		self.response.out.write(template.render(helpers.get_path("message"), template_values))

class MainPage(webapp.RequestHandler):
	def get(self):
		template_values = helpers.get_liu_stuff(self.request.uri)
		self.response.out.write(template.render(helpers.get_path("index"), template_values))

class Preferences(webapp.RequestHandler):
	def get(self, errors = []):
		template_values = helpers.get_liu_stuff(self.request.uri)
		liu = template_values["liu"]

		if not liu:
			self.redirect(template_values["login_url"])
			return

		prefs = op_models.UserOptions.get_prefs(liu)

		template_values["email"] = prefs.email
		template_values["email_on_msg"] = prefs.prefs & op_models.UserOptions.SEND_EMAIL_ON_MESSAGE
		template_values["email_on_start"] = prefs.prefs & op_models.UserOptions.SEND_EMAIL_ON_GAME_START

		if errors != []:
			template_values["errors"] = errors

		self.response.out.write(template.render(helpers.get_path("prefs"), template_values))
	
	def post(self):
		liu = users.get_current_user()

		if not liu:
			self.redirect("/")
			return

		email = self.request.get("email")
		if not re.compile("[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+").match(email):
			self.get(["Email is not valid."])
			return

		values = { "email_on_msg": op_models.UserOptions.SEND_EMAIL_ON_MESSAGE,
		           "email_on_start": op_models.UserOptions.SEND_EMAIL_ON_GAME_START
				 }
		
		prefs_int = 0
		for k,v in values.iteritems():
			if self.request.get(k):
				prefs_int |= v

		prefs = op_models.UserOptions.get_prefs(liu)

		prefs.email = email
		prefs.prefs = prefs_int
		prefs.put()

		s = session.Session()
		s["message"] = "Preferences saved."

		self.redirect("/message")


